package drone.basis.util ;

import java.math.BigDecimal ;
import java.util.List ;

public class ExcelUtil
{

	public static String excel (
			String [ ] fieldHead ,
			List < String [ ] > fieldBody
	) throws Exception
	{

		StringBuffer sb = new StringBuffer ( ) ;
		sb.append ( "<html>" ) ;
		sb.append ( "<head>" ) ;
		sb.append ( "<meta http-equiv=Content-Type content='text/html;charset=utf-8'>" ) ;
		sb.append ( "</head><body>" ) ;
		sb.append ( "<table cellspacing='0' cellpadding='5' rules='all' border='1'>" ) ;
		sb.append ( "<tr style='font-weight: bold; white-space: nowrap;'>" ) ;
		for ( String head : fieldHead )
		{
			sb.append ( "<td  style='vnd.ms-excel.numberformat:@'>" + head + "</td>" ) ;
		}
		sb.append ( "</tr>" ) ;

		for ( String [ ] data : fieldBody )
		{
			sb.append ( "<tr>" ) ;
			for ( String item : data )
			{
				if ( StringUtil.isEmpty ( item ) )
				{
					sb.append ( "<td  style='vnd.ms-excel.numberformat:@'></td>" ) ;
				} else
				{
					sb.append ( "<td  style='vnd.ms-excel.numberformat:@'>" + item + "</td>" ) ;
				}
			}
			sb.append ( "</tr>" ) ;
		}
		sb.append ( "</table>" ) ;
		sb.append ( "</body></html>" ) ;
		return sb.toString ( ) ;
	}

	public static String excel (
			String [ ] fieldHead ,
			List < String [ ] > fieldBody ,
			Integer [ ] fieldFoot
	) throws Exception
	{

		StringBuffer sb = new StringBuffer ( ) ;
		sb.append ( "<html>" ) ;
		sb.append ( "<head>" ) ;
		sb.append ( "<meta http-equiv=Content-Type content='text/html;charset=utf-8'>" ) ;
		sb.append ( "</head><body>" ) ;
		sb.append ( "<table cellspacing='0' cellpadding='5' rules='all' border='1'>" ) ;
		sb.append ( "<tr style='font-weight: bold; white-space: nowrap;'>" ) ;
		for ( String head : fieldHead )
		{
			sb.append ( "<td  style='vnd.ms-excel.numberformat:@'>" + head + "</td>" ) ;
		}
		sb.append ( "</tr>" ) ;

		for ( String [ ] data : fieldBody )
		{
			sb.append ( "<tr>" ) ;

			for ( int i = 0 ; i < data.length ; i ++ )
			{
				String item = data [ i ] ;
				if ( StringUtil.isEmpty ( item ) )
				{
					sb.append ( "<td  style='vnd.ms-excel.numberformat:@'></td>" ) ;
				} else
				{
					sb.append ( "<td  style='vnd.ms-excel.numberformat:@'>" + item + "</td>" ) ;
				}
			}
			sb.append ( "</tr>" ) ;
		}
		sb.append ( "<tr style='font-weight: bold; white-space: nowrap;'>" ) ;

		for ( int i = 0 ; i < fieldHead.length ; i ++ )
		{

			boolean match = false ;
			for ( Integer foot : fieldFoot )
			{
				if ( foot == i )
				{
					sb.append ( "<td  style='vnd.ms-excel.numberformat:@'>" + add ( fieldBody , i ) + "</td>" ) ;
					match = true ;
				}
			}
			if ( ! match )
			{
				sb.append ( "<td  style='vnd.ms-excel.numberformat:@'></td>" ) ;
			}

		}

		sb.append ( "</tr>" ) ;
		sb.append ( "</table>" ) ;
		sb.append ( "</body></html>" ) ;
		return sb.toString ( ) ;
	}
	
	public static String excel (
			String [ ] fieldHead ,
			List < String [ ] > fieldBody ,
			Integer [ ] fieldFoot,
			String [ ] fieldInclude 
	) throws Exception
	{

		StringBuffer sb = new StringBuffer ( ) ;
		sb.append ( "<html>" ) ;
		sb.append ( "<head>" ) ;
		sb.append ( "<meta http-equiv=Content-Type content='text/html;charset=utf-8'>" ) ;
		sb.append ( "</head><body>" ) ;
		sb.append ( "<table cellspacing='0' cellpadding='5' rules='all' border='1'>" ) ;
		sb.append ( "<tr style='font-weight: bold; white-space: nowrap;'>" ) ;
		for ( String head : fieldHead )
		{
			sb.append ( "<td  style='vnd.ms-excel.numberformat:@'>" + head + "</td>" ) ;
		}
		sb.append ( "</tr>" ) ;

		for ( String [ ] data : fieldBody )
		{
			sb.append ( "<tr>" ) ;

			for ( int i = 0 ; i < data.length ; i ++ )
			{
				String item = data [ i ] ;
				if ( StringUtil.isEmpty ( item ) )
				{
					sb.append ( "<td  style='vnd.ms-excel.numberformat:@'></td>" ) ;
				} else
				{
					sb.append ( "<td  style='vnd.ms-excel.numberformat:@'>" + item + "</td>" ) ;
				}
			}
			sb.append ( "</tr>" ) ;
		}
		sb.append ( "<tr style='font-weight: bold; white-space: nowrap;'>" ) ;

		for ( int i = 0 ; i < fieldHead.length ; i ++ )
		{

			boolean match = false ;
			for ( Integer foot : fieldFoot )
			{
				if ( foot == i )
				{
					sb.append ( "<td  style='vnd.ms-excel.numberformat:@'>" + add ( fieldBody , i ) + "</td>" ) ;
					match = true ;
				}
			}
			if ( ! match )
			{
				sb.append ( "<td  style='vnd.ms-excel.numberformat:@'></td>" ) ;
			}

		}

		sb.append ( "</tr>" ) ;
		sb.append ( "</table>" ) ;
		sb.append ( "</body></html>" ) ;
		return sb.toString ( ) ;
	}

	public static String add (
			List < String [ ] > fieldBody ,
			Integer col
	)
	{

		BigDecimal result = new BigDecimal ( 0 ) ;

		try
		{

			for ( String [ ] data : fieldBody )
			{
				for ( int i = 0 ; i < data.length ; i ++ )
				{
					String item = data [ i ] ;

					if ( i == col )
					{
						result = result.add ( new BigDecimal ( item ) ) ;
					}

				}
			}
		} catch ( Exception e )
		{
			return "" ;
		}

		return result.toPlainString ( ) ;
	}
}
